package com.example.tenant.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.tenant.entity.SysRGroupRole;
import com.example.tenant.mapper.SysRGroupRoleMapper;
import com.example.tenant.service.SysRGroupRoleService;
import lombok.SneakyThrows;
import org.springframework.stereotype.Service;

import java.io.File;
import java.nio.file.Files;
import java.util.List;

/**
 * 行政区划
 *
 * @author waterx
 * @date 2024-02-16 22:40:06
 */
@Service
public class SysRGroupRoleServiceImpl extends ServiceImpl<SysRGroupRoleMapper, SysRGroupRole> implements SysRGroupRoleService {

    @SneakyThrows
    @Override
    public void export(List<String> roleIds, String filePath) {
        if (roleIds.size() > 0) {
            // 生成文件路径
            String fileName = "sys_r_group_role.xlsx";
            String fullPath = filePath + File.separator + fileName;
            File excelFile = new File(fullPath);

            List<SysRGroupRole> data = this.list(new LambdaQueryWrapper<SysRGroupRole>().in(SysRGroupRole::getRoleId, roleIds));

            try (ExcelWriter excelWriter = EasyExcel.write(Files.newOutputStream(excelFile.toPath()), entityClass).build()) {
                // 创建工作表
                WriteSheet writeSheet = EasyExcel.writerSheet("sheet").build();
                // 写入数据
                excelWriter.write(data, writeSheet);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
